﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Smarly.Idml.RelaxNg.Generator.Model;
using Tenuto.Grammar;

namespace Smarly.Idml.RelaxNg.Generator.Proxy
{
	public class ChoiceExpBuilder : Builder<ChoiceExp>
	{
		[DebuggerStepThrough]
		protected internal ChoiceExpBuilder(ChoiceExp exp, Action<IObjectName> updateName) : base(exp, updateName) { }

		public override IStruct Fill(IStruct getStruct)
		{
			getStruct = base.Fill(getStruct);

			if (getStruct is EnumStruct)
			{
				bool condition = Exp.children.All(x => x is ValueExp);
				if (!condition)
				{
					//fix remove continue
					return getStruct;
					throw new NotImplementedException();
				}

				IEnumerable<ValueExp> valueExps = Exp.children.Cast<ValueExp>();
				List<string> values = valueExps.Select(x => x.value).Cast<string>().ToList();

				((EnumStruct)getStruct).Values = values;
			}
			else if (getStruct is ObjectStruct)
			{
				var obj = (ObjectStruct)getStruct;

				var el = Factory(Exp.exp1, UpdateName).Fill();

				//todo remove condition
				if (el != null)
				{
					el.IsNull = true;

					obj.Structs.Add(el);
				}
			}
			else if (getStruct is AttrStruct)
			{
				//todo not implemented
			}
			else if (getStruct is ListStruct)
			{
				//todo not implemented
			}
			else
			{
				//todo remove condition
				if (getStruct == null)
					return getStruct;

				throw new NotImplementedException("Not implement for type - " + getStruct.GetType().FullName);
			}

			return getStruct;
		}

		public override IStruct CreateIStruct()
		{
			IStruct getStruct = new EnumStruct();
			return Fill(getStruct);
		}
	}
}